package com.xunk.equipment.controller;

import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.equipment.service.zhijia.service.ZhijiaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @ProjectName: xunk
 * @Package: com.xunk.equipment.controller
 * @ClassName: ZhijiaController
 * @Author: luanzhiwei
 * @Description: 智家接口
 * @Date: 2021/4/6 15:19
 * @Version: 1.0
 */
@RestController
@RequestMapping(value = "/zhijia/")
public class ZhijiaController extends BaseController {

    @Autowired
    private ZhijiaService zhijiaService;
    /*智家授权，先调用查询授权状态接口，若返回参数为false，则调用获取登录url接口获取智家的登录url-此接口会传给智家回调我们的url，
    用户登录成功后，智家回调我们页面，我们页面调用接口获取token并在后台缓存绑定，返回处理成功之后，前端即可调用后续查询设备列表，控制设备等接口*/

    /**
     * 查询授权状态
     *
     * @return
     */
    @GetMapping("/authStatus")
    public AjaxResult authStatus(@RequestParam(name = "homeId") Long homeId) {
        return zhijiaService.authStatus(homeId, super.getUserId());
    }

    /**
     * 获取登录url
     *
     * @return
     */
    @GetMapping("/getLoginUrl")
    public AjaxResult getLoginUrl(@RequestParam(name = "homeId") Long homeId) {
        return zhijiaService.getLoginUrl(homeId, super.getToken());
    }


    /**
     * 登录回调地址
     *
     * @param code
     * @param state
     * @return
     */
    @GetMapping(value = "/loginCallback")
    public AjaxResult getToken(@RequestParam(name = "code") String code, @RequestParam(name = "state") String state) {
        return zhijiaService.loginCallback(code, state);
    }

    /**
     * 刷新设备列表接口
     * 可用于获取用户全部设备
     *
     * @param homeId
     * @return
     */
    @GetMapping(value = "/deviceInfos")
    public AjaxResult deviceInfos(@RequestParam(name = "homeId") Long homeId) {
        return zhijiaService.deviceInfos(homeId);
    }

    /**
     * 查询设备详情接口
     *
     * @param deviceSn
     * @return
     */
    @GetMapping(value = "/deviceDetail")
    public AjaxResult deviceDetail(@RequestParam(name = "homeId") Long homeId, @RequestParam(name = "deviceSn") String deviceSn) {
        return zhijiaService.deviceDetail(homeId, deviceSn);
    }

    /**
     * 查询单个设备的状态
     *
     * @param deviceSn
     * @param part     过滤参数；查询结果一定返回shadowBaseInfo部分；通过过滤参数控制返回其他部分；等于1时，其他只返回reported部分；等于2时，其他只返回desired部分；等于0时，其他部分全部返回；默认值为0；
     * @return
     */
    @GetMapping(value = "/deviceStatus")
    public AjaxResult deviceStatus(@RequestParam(name = "homeId") Long homeId,
                                   @RequestParam(name = "deviceSn") String deviceSn,
                                   @RequestParam(name = "part", required = false) String part) {
        return zhijiaService.deviceStatus(homeId, deviceSn, part);
    }

    /**
     * 查询开通预授权的型号列表
     *
     * @return
     */
    @PostMapping(value = "/deviceModels")
    public AjaxResult deviceModels(@RequestParam(name = "homeId") Long homeId) {
        return zhijiaService.deviceModels(homeId);
    }

}
